Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing argc and argv to target program #185

Merged
merged 4 commits into from
Aug 9, 2023
Merged

Support passing argc and argv to target program #185

merged 4 commits into from
Aug 9, 2023

Conversation

ChinYikMing
Copy link
Collaborator

@ChinYikMing ChinYikMing commented Aug 6, 2023

Half close #150, I left envp in TODO

I assumed all emulator's option are passed before the target program and its args thus I modify the parse_args function consequently

src/main.c Fixed Show fixed Hide fixed
src/main.c Fixed Show fixed Hide fixed
src/main.c Fixed Show fixed Hide fixed
src/main.c Fixed Show fixed Hide fixed
@ChinYikMing ChinYikMing requested a review from jserv August 6, 2023 23:06
Makefile Outdated Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve the concerns raised by CodeQL.

src/main.c Outdated Show resolved Hide resolved
src/riscv.c Outdated Show resolved Hide resolved
tests/args.c Outdated Show resolved Hide resolved
@jserv jserv requested a review from qwe661234 August 7, 2023 03:06
@ChinYikMing ChinYikMing requested a review from jserv August 7, 2023 16:39
- half close #150, I left envp in TODO
- refactor parse_args function with getopt to support
  short option which looks cleaner and replace all long
  option with short option
- update README
src/riscv.c Outdated Show resolved Hide resolved
src/riscv.c Outdated Show resolved Hide resolved
src/riscv.c Outdated Show resolved Hide resolved
@ChinYikMing ChinYikMing requested a review from jserv August 8, 2023 02:20
@qwe661234
Copy link
Collaborator

In file src/risc.c, the memory_write has been invoked twice to wrtie argc or args to memory (according to your comments). Why we need to maintain two copies of argc or args in virtual memory?

@ChinYikMing
Copy link
Collaborator Author

In file src/risc.c, the memory_write has been invoked twice to wrtie argc or args to memory (according to your comments). Why we need to maintain two copies of argc or args in virtual memory?

If we are running emulator on 64-bit machine, the virtual address would be 64-bit unless it is compile with -m32 option. Since we are RV32 emulator, so the address space should be < 4GB, from this point we cannot directly access 64-bit address from emulator. Thus, first call of memory write store the argc and argv to < 4GB address, and second call of memory write for RV32 ABI.

@qwe661234
Copy link
Collaborator

qwe661234 commented Aug 8, 2023

If we are running emulator on 64-bit machine, the virtual address would be 64-bit unless it is compile with -m32 option. Since we are RV32 emulator, so the address space should be < 4GB, from this point we cannot directly access 64-bit address from emulator. Thus, first call of memory write store the argc and argv to < 4GB address, and second call of memory write for RV32 ABI.

Ok, but I still confuse. I guess these two copies are for different purposes. Could you explain the different purpose between store the argc and argv to < 4GB address and memory write for RV32 ABI?(or maybe leave some comments) Thx.

@ChinYikMing
Copy link
Collaborator Author

ChinYikMing commented Aug 8, 2023

Ok, but I still confuse. I guess these two copies are for different purposes. Could you explain the different purpose between store the argc and argv to < 4GB address and memory write for RV32 ABI?(or maybe leave some comments) Thx.

For example, we want access the args[0], the instruction might look like this: lw a0, 4(sp). In mapping, emulator will call memory_read_w. From implementation of memory_read_w, it is base memory + "offset", this "offset" should within the memory size otherwise we get segmentation fault. Thus, value stored in sp+4 should be the offset to the args[0]. The first copy of argc and argv mainly for maintaining the offset and making sure it is < 4GB.

@qwe661234
Copy link
Collaborator

qwe661234 commented Aug 8, 2023

For example, we want access the args[0], the instruction might look like this: lw a0, 4(sp). In mapping, emulator will call memory_read_w. From implementation of memory_read_w, it is base memory + "offset", this "offset" should within the memory size otherwise we get segmentation fault. Thus, value stored in sp+4 should be the offset to the args[0]. The first copy of argc and argv mainly for maintaining the offset and making sure it is < 4GB.

Does you mean the instruction lw a0, 4(sp) get the offset of memory address of args[0], and the data of args[0] store in this memory address(mem_base + offset)?

@ChinYikMing
Copy link
Collaborator Author

ChinYikMing commented Aug 8, 2023

Does you mean the instruction lw a0, 4(sp) get the offset of memory address of args[0], and the data of args[0] store in this memory address(mem_base + offset)?

yes

@ChinYikMing
Copy link
Collaborator Author

ChinYikMing commented Aug 8, 2023

Resolve the concerns raised by CodeQL.

I think I have solved it.

@jserv
Copy link
Contributor

jserv commented Aug 8, 2023

I defer to @qwe661234 for confirmation.

Makefile Outdated Show resolved Hide resolved
@jserv jserv merged commit 0eae191 into sysprog21:master Aug 9, 2023
13 checks passed
@jserv
Copy link
Contributor

jserv commented Aug 9, 2023

Thank @ChinYikMing for contributing!

@ChinYikMing ChinYikMing deleted the pr branch August 9, 2023 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle argc/argv/envp properly
3 participants